<?php

class Emms_Form_Loan_ChangeDates extends HTech_Form
{
    private $_maxFirstPaymentDate; 
    private $_loanProductParams;
    
    public function getValues()
    {
        return parent::getValues() + $this->_loanProductParams;
    }
    
    private function _setLoanProductParams($loanProductId)
    {
        if($this->_request->isPost()) {
            $products = new LoanProducts();
            try {
                $this->_loanProductParams = $products->getRecord($loanProductId)->d->toArray();
            } catch (Exception $e) {
                $e = $e;
            }
        }
    }
    
    private function _setMaxFirstPaymentDate()
    {
        if($this->_request->isPost()) {
            try {
                $this->_maxFirstPaymentDate = Emms_PmtPlan::getMaxFirstPaymentDate(
                    $this->_request->getPost('TentativeDeliveryDate'),  
                    $this->_loanProductParams['PrincipalGraceMargin'],
                    $this->_loanProductParams['PaymentFrequency']);
            } catch (Exception $e) {
                $e = $e; 
            }
        }
    }
    
    public function __construct($master) 
    {
        parent::__construct(); 
        $this->_setLoanProductParams($master->LoanProductId);
        $this->_setMaxFirstPaymentDate();   
        
        $this->setName('ChangeDates');
        
        $this->setAttrib('title', $this->translate("Loan application"));
        $this->setDescription($this->translate("Change disbursement and first payment dates"));
        
        //$loanMasterId = new Zend_Form_Element_Hidden('LoanMasterId');
          
        $deliveryDate = new Zend_Form_Element_Text('TentativeDeliveryDate');
        $deliveryDate->setLabel("Disbursement date")
            ->setRequired(true)
            ->addPrefixPath('HTech_Validate', 'HTech/Validate', 'validate')
            ->setValue($master->TentativeDeliveryDate)
            ->addValidator('Date', true, array(
                'weekday' => true, 
                'maxDate' => $this->_request->getPost('TentativeFirstPaymentDate'),
                'messages' => array(
                    'msgWeekday' => $this->translate("Weekend dates not allowed"),
                    'msgMaxDateStrict' => $this->translate("Non valid date or date range"))));
        
        $firstPaymentDate = new Zend_Form_Element_Text('TentativeFirstPaymentDate');
        $firstPaymentDate->setLabel("First payment date")
            ->setRequired(true)
            ->addPrefixPath('HTech_Validate', 'HTech/Validate', 'validate')
            ->setValue($master->TentativeFirstPaymentDate)
            ->addValidator('Date', true, array(
                'weekday' => true, 
                'minDate' => $this->_request->getParam('TentativeDeliveryDate'),
                'messages' => array(
                    'msgWeekday' => $this->translate("Weekend dates not allowed"),
                    'msgMinDateStrict' => $this->translate("Non valid date or date range"))))
            ->addValidator('Callback', true, array(
                'callback' => array($this, 'checkGraceMarginCallback'),
                'messages' => sprintf($this->translate("Principal grace margin invalid or exceeded. Maximum allowed: %s"), $this->_maxFirstPaymentDate))); 
                        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type' => 'submit')));
                
        $this->addElements(array($deliveryDate, $firstPaymentDate, $submit));
    }
    
    public function checkGraceMarginCallback($date, $args)
    {
        if(!Zend_Date::isDate($args['TentativeDeliveryDate'], 'YYYY-MM-DD')) return true;
        if($args['TentativeDeliveryDate']>=$date) return false;
        if($date > $this->_maxFirstPaymentDate) return false;
        return true;
    }  
    
    /**
     * Returns false if $date is hollidays, malformed or invalid. Return true otherwise.
     * 
     * @param type $date Format YYYY-MM-DD
     * @return boolean
     */
    public function checkHollidaysCallback($date)
    {
        try {
            $hollidays = Emms_Date::isHollidays($date);
        } catch (Exception $e) {
            $e = $e;
            $hollidays = true;
        }
        if($hollidays) return false;
        return true; 
    }  
}